package com.stefan.stack;

import java.util.Scanner;

//栈结构的话是LIFO - 用单向链表实现的话：（头插法 + 头pop）或者 (尾插法+尾pop)
//显然头插法和头pop更好做一些
public class LinkedListStefanStack<E> implements StefanStack<E>{

    private int size;
    private Node<E> head;

    public LinkedListStefanStack () {
        size = 0;
    }

    static class Node<E> {
        private E data;
        private Node<E> next;

        public Node (E data) {
            this.data = data;
            next = null;
        }

    }

    @Override
    public void push(E e) {
        //这里采用头插法
        Node<E> node = new Node<>(e);
        node.next = head; // 头插法
        head = node; // 把当前节点设置为head
        size++;
    }

    @Override
    public E pop() {
        E result = null;
        if (size > 0) {
            result = head.data;
        }
        size--;
        return result;
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }
    public static void main(String[] args) {
        StefanStack<Character> sb = new LinkedListStefanStack<>();
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String s = scanner.next();
            System.out.println(sb.charsMatcher(s));
        }

    }
}
